/**
 * @file test_gb28181_config.cpp
 * @brief GB28181配置测试示例
 * 
 * 这个文件展示了如何使用新实现的GB28181配置API
 */

#include <iostream>
#include <nlohmann/json.hpp>
#include "net_command_handler.h"

using namespace El::Protocol;
using json = nlohmann::json;

int main() {
    NetCommandHandler handler;
    
    // 测试获取GB28181配置
    std::cout << "=== 测试获取GB28181配置 ===" << std::endl;
    json getParams = json::object();
    json getResult = handler.HandleGB28181GetConfig("test-get-1", getParams);
    std::cout << "获取结果: " << getResult.dump(2) << std::endl;
    
    // 测试设置GB28181配置（启用双服务器）
    std::cout << "\n=== 测试设置GB28181配置（启用双服务器） ===" << std::endl;
    json setParams = {
        {"enabled", true},
        {"sip_server", json::array({
            {
                // 第一个SIP服务器
                {"channel_count", 2},
                {"civil_code", "340200"},
                {"device_id", "34020000001320000001"},
                {"channels", json::array({
                    {
                        {"channel_id", "34020000001320000001"},
                        {"channel_name", "前门摄像头"}
                    },
                    {
                        {"channel_id", "34020000001320000002"},
                        {"channel_name", "后门摄像头"}
                    }
                })},
                {"domain", "3402000000"},
                {"heartbeat_interval", 60},
                {"local_port", 5060},
                {"max_timeout_count", 3},
                {"password", "12345678"},
                {"register_interval", 3600},
                {"register_expires", 3600},
                {"server_id", "34020000002000000001"},
                {"server_ip", "192.168.1.100"},
                {"server_port", 5060}
            },
            {
                // 第二个SIP服务器
                {"channel_count", 2},
                {"civil_code", "340200"},
                {"device_id", "34020000001320000003"},
                {"channels", json::array({
                    {
                        {"channel_id", "34020000001320000003"},
                        {"channel_name", "侧门摄像头"}
                    },
                    {
                        {"channel_id", "34020000001320000004"},
                        {"channel_name", "停车场摄像头"}
                    }
                })},
                {"domain", "3402000000"},
                {"heartbeat_interval", 60},
                {"local_port", 5061},
                {"max_timeout_count", 3},
                {"password", "87654321"},
                {"register_interval", 3600},
                {"register_expires", 3600},
                {"server_id", "34020000002000000002"},
                {"server_ip", "192.168.1.101"},
                {"server_port", 5061}
            }
        })}
    };
    
    json setResult = handler.HandleGB28181SetConfig("test-set-1", setParams);
    std::cout << "设置结果: " << setResult.dump(2) << std::endl;
    
    // 测试设置GB28181配置（禁用）
    std::cout << "\n=== 测试设置GB28181配置（禁用） ===" << std::endl;
    json disableParams = {
        {"enabled", false},
        {"sip_server", json::array()}
    };
    
    json disableResult = handler.HandleGB28181SetConfig("test-set-2", disableParams);
    std::cout << "禁用结果: " << disableResult.dump(2) << std::endl;
    
    // 测试参数验证
    std::cout << "\n=== 测试参数验证 ===" << std::endl;
    json invalidParams = {
        {"enabled", true},
        {"sip_server", json::array({
            {
                {"device_id", "invalid_id"}, // 错误的device_id长度
                {"server_ip", "192.168.1.100"}
            }
        })}
    };
    
    json invalidResult = handler.HandleGB28181SetConfig("test-invalid", invalidParams);
    std::cout << "无效参数结果: " << invalidResult.dump(2) << std::endl;
    
    // 测试超过2个服务器的错误处理
    std::cout << "\n=== 测试超过2个服务器的错误处理 ===" << std::endl;
    json tooManyServersParams = {
        {"enabled", true},
        {"sip_server", json::array({
            {
                {"channel_count", 1},
                {"civil_code", "340200"},
                {"device_id", "34020000001320000001"},
                {"domain", "3402000000"},
                {"password", "12345678"},
                {"server_id", "34020000002000000001"},
                {"server_ip", "192.168.1.100"},
                {"server_port", 5060}
            },
            {
                {"channel_count", 1},
                {"civil_code", "340200"},
                {"device_id", "34020000001320000002"},
                {"domain", "3402000000"},
                {"password", "12345678"},
                {"server_id", "34020000002000000002"},
                {"server_ip", "192.168.1.101"},
                {"server_port", 5061}
            },
            {
                {"channel_count", 1},
                {"civil_code", "340200"},
                {"device_id", "34020000001320000003"},
                {"domain", "3402000000"},
                {"password", "12345678"},
                {"server_id", "34020000002000000003"},
                {"server_ip", "192.168.1.102"},
                {"server_port", 5062}
            }
        })}
    };
    
    json tooManyResult = handler.HandleGB28181SetConfig("test-too-many", tooManyServersParams);
    std::cout << "超过2个服务器结果: " << tooManyResult.dump(2) << std::endl;
    
    return 0;
}
